package shoot;

import java.awt.event.KeyEvent;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.net.URL;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.imageio.ImageIO;
import javax.management.RuntimeErrorException;

public class Personnage {
	List<BufferedImage> face;
	List<BufferedImage> profileG;
	List<BufferedImage> profileD;
	List<BufferedImage> derriere;
	private Map<Integer,List<BufferedImage>> map ;
	private final int wref = 19;
	private final int href = 31;
	private final int wEspacement = 4;
	private int dx;
	private int dy;
	private int speed = 3;
	private int imageNumber = 0 ;
	private int currentKeyEvent ;
	public Personnage(String fileName, int dxInit, int dyInit) {
		
		try {
		URL url = this.getClass().getResource("/img/" + fileName);
		BufferedImage img = ImageIO.read(url);
		face = new ArrayList<BufferedImage>(4);
		profileG = new ArrayList<BufferedImage>(4);
		profileD = new ArrayList<BufferedImage>(4);
		derriere = new ArrayList<BufferedImage>(4);
		int wdepart = 3;
		int hdepart = 0;
		System.out.println("width: " + img.getWidth(null) + " - height: "
				+ img.getHeight(null));
		for (int ii = 0; ii < 3; ii++) {
			if(ii==1) {
				derriere.add(0,img.getSubimage(wdepart, hdepart, wref, href));
			}
			derriere.add(img.getSubimage(wdepart, hdepart, wref, href));			
			wdepart += wref + wEspacement;
		}
		wdepart = 3;
		hdepart += href + 1;
		for (int ii = 0; ii < 3; ii++) {
			if(ii==1) {
				profileD.add(0,img.getSubimage(wdepart, hdepart, wref, href));
			}
			profileD.add(img.getSubimage(wdepart, hdepart, wref, href));
			wdepart += wref + wEspacement;
		}
		wdepart = 3;
		hdepart += href + 1;
		for (int ii = 0; ii < 3; ii++) {	
			if(ii==1) {
				face.add(0,img.getSubimage(wdepart, hdepart, wref, href));
			}
			face.add(img.getSubimage(wdepart, hdepart, wref, href));
			wdepart += wref + wEspacement;
		}
		wdepart = 3;
		hdepart += href + 1;
		for (int ii = 0; ii < 3; ii++) {
			if(ii==1) {
				profileG.add(0,img.getSubimage(wdepart, hdepart, wref, href));
			}
			profileG.add(img.getSubimage(wdepart, hdepart, wref, href));
			wdepart += wref + wEspacement;
		}

		this.dx = dxInit;
		this.dy = dyInit;
		
		map = new HashMap<Integer, List<BufferedImage>>(4);
		map.put(KeyEvent.VK_DOWN, face);
		map.put(KeyEvent.VK_UP, derriere);
		map.put(KeyEvent.VK_LEFT, profileG);
		map.put(KeyEvent.VK_RIGHT, profileD);
		currentKeyEvent = KeyEvent.VK_DOWN ;
		}
		catch (IOException e) {			
			throw new RuntimeException("Impossible de récuperer l'image "+fileName,e);
		}
	}
	
	public void keyPressed(KeyEvent e) {
		System.out.println("KeyCode: "+e.getKeyCode());
		switch (e.getKeyCode()) {
		case KeyEvent.VK_DOWN:
			dy+=speed;
			update(e);
			break;
		case KeyEvent.VK_UP:
			dy-=speed;
			update(e);
			break;
		case KeyEvent.VK_LEFT:
			dx-=speed;
			update(e);
			break;
		case KeyEvent.VK_RIGHT:
			dx+=speed;
			update(e);
			break;
		}
		System.out.println("dx: "+dx+" - dy: "+dy);
	}
	
	
	public void update(KeyEvent e) {
		if(e.getKeyCode()==currentKeyEvent) {
			++imageNumber;
		} else{
			currentKeyEvent = e.getKeyCode();
			imageNumber = 0;
		}
	}
		
	public BufferedImage getImage(){
		return map.get(currentKeyEvent).get(imageNumber%4);
	}
	public int getDx() {
		return dx;
	}
	public int getDy() {
		return dy;
	}
}
